import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'

Vue.use(Vuex)

// 创建仓库store实例
const store = new Vuex.Store({
  state: {
    fold: false, // 默认是展开的
    isRefresh: true, // 控制页面刷新状态
    isDrawerOpen: false, // 控制抽屉开关
    token: '',
    refreshToken: '',
    username: '',
    isShowLogo: true,
    isShowRefresh: true,
    isShowFullScreen: true,
    language: 'zh',
  },
  mutations: {
    changeLanguage(state, language) {
      // console.log('changeLanguage')
      state.language = language
    },
    toggleDrawer(state) {
      state.isDrawerOpen = !state.isDrawerOpen
    },
    toggleRefresh(state) {
      state.isRefresh = !state.isRefresh
    },
    toggleFold(state) {
      state.fold = !state.fold
    },
    setToken(state, { token, refreshToken }) {
      state.token = token
      state.refreshToken = refreshToken
    },
    setUsername(state, username) {
      state.username = username
    },

    clearToken(state) {
      state.token = ''
      state.refreshToken = ''
    },
    clearUsername(state) {
      state.username = ''
    },
  },
  plugins: [createPersistedState()],
})

store.subscribe((mutation, state) => {
  console.log('subscribe')
  console.log(mutation.type)
  console.log(mutation.payload)
  console.log(state)
})

// 导出仓库
export default store
